home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <process.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys\types.h>
- #include <sys\stat.h>
- #include <dgb.h>
- #include <exc.h>
- #include "types.h"
- #include "profile.h"
- #include "segments.h"
- #include "symfile.h"
- #include "segbug.h"
-
- PSTR OutputFileName = NULL;
- PSTR RunFile = "WIN2";
- PSTR TimerPeriodString = NULL;
- WORD TimerPeriod = 100;
-
- char Usage[] = "Usage: %s [-?] [-t] [-o] [-x] [Symbol files ...] [WIN2.COM argument]\n"
- "-t Time in milliseconds (default 100ms)\n"
- "-o Output file (defaults to stdout)\n"
- "-x Program to run (default WIN2.COM)\n"
- "Symbol files must end in '.SYM'\n"
- "This program requires PC-DOS v3.x/IBM PC-AT or compatible\n\n"
- "Windows execution profiler V1.03 DGB\n\n";
-
- char Copyright[] =
- "Execution Timer for Microsoft Windows v1.0\n\
- (c) Duncan G. Booth RCP Ltd. 1987\n\n\
- \tRCP Ltd.\n\
- \tBrookside\n\
- \tWestbrook Street\n\
- \tBlewbury\n\
- \tDidcot\n\
- \tOxon OX11 9QA\n\
- \tUnited Kingdom\n\n\
- Permission is granted to copy this program for non-commercial use only.\n\
- For commercial distribution, update information or program source please\n\
- contact D.Booth at the above address or 'jrichards' on BIX.\n";
-
- exception BAD_ARGS = "Bad arguments";
- exception BAD_SYMBOL = "Cannot find symbol!";
- exception FINDFILE_FAILED = "Cannot find file";
-
- void *emalloc(WORD);
- PSTR safestrdup(PSTR);
-
- void *emalloc(w)
- WORD w;
- {
- char *p = malloc(w);
- if (p == NULL) exc_raise(OUT_OF_MEMORY);
- return p;
- }
- char *safestrdup(s)
- register char *s;
- {
- s = strdup(s);
- if (s == NULL) exc_raise (OUT_OF_MEMORY);
- return s;
- }
-
- #define FNAMESIZE 128
- char *pathseps = "\\:/"; /* Valid pathname separators */
- char *pathdelim = "; "; /* Environment path delimiters */
- /* This procedure will find a file matching the given name.
- a) The name is extended with 'ext' unless it already has an extension or
- force is specified. (ext should include the '.')
- b) The current directory is checked for the file
- c) Unless the file has a path associated, the directories in paths are
- searched for the file. paths is a semicolon separated list.
- Flags: force - strip existing extension and add new.
- output - file is for output. Just do extension processing.
- */
-
- char *findfile (paths, name, ext, force, output)
- char *paths, *name, *ext;
- int force, output;
- {
- register char *p, *q;
- int namelen;
- int haspath = FALSE;
- char filename[FNAMESIZE]; /* Filename without path. */
- char longname[FNAMESIZE]; /* Filename with path. */
- struct stat dummy;
- strcpy(filename, name);
- for (p = q = filename; (q = strpbrk(p, pathseps)) != NULL; p = q+1)
- haspath = TRUE;
-
- /* p points to start of name part of filename */
- if ((q = strchr(p, '.')) == NULL || force)
- {
- if (q != NULL) *q = '\0'; /* Remove old extension */
- strcat(p, ext);
- }
- if (output || stat(filename, &dummy) == 0) return strlwr(safestrdup (filename));
-
- if (haspath || paths == NULL)
- {
- BadFile = name;
- exc_raise (FINDFILE_FAILED);
- }
-
- paths = safestrdup(paths); /* Because strtok splats this string. */
-
- /* Now search path string: */
- p = strtok(paths, pathdelim);
- do
- {
- strcpy(longname, p);
- strcat(longname, "\\");
- strcat(longname, filename);
- if (stat(longname, &dummy) == 0)
- {
- free(paths);
- return strlwr(safestrdup (longname));
- }
- } while ((p = strtok(NULL, pathdelim)) != NULL);
- free(paths);
- BadFile = name;
- exc_raise(FINDFILE_FAILED);
- }
-
- /*
- * Although this program is compiled SS != DS, functions outside the
- * debug internals do actually have SS==DS. In particular this means
- * I can call system from here.
- */
- void main (argc, argv)
- int argc;
- char **argv;
- {
- char *path = getenv ("PATH");
- char *progname = argv[0];
- char cmdline[128];
- BEGIN
- {
- register char *s;
- register char *q;
- for (s = argv[0]; (q = strpbrk(s, ":/\\")) != NULL;) s = q+1;
-
- if (*s != 0)
- {
- progname = s = strlwr(safestrdup(s));
- if ((q = strchr(s, '.')) != NULL) *q = 0;
- }
- }
-
- if (_osmajor < 3) exc_raise(BAD_ARGS);
- while (argc > 1 && argv[1][0] == '-')
- {
- char *p, **pp;
- switch (argv[1][1])
- {
- case 0: exc_raise(BAD_ARGS);
- case 'x': case 'X':
- pp = &RunFile;
- break;
- case 'o': case 'O':
- pp = &OutputFileName;
- break;
- case 't': case 'T':
- pp = &TimerPeriodString;
- break;
- case '?':
- fprintf(stderr, Usage, progname);
- fprintf(stderr, Copyright);
- exit(0);
- }
- *pp = &argv[1][2];
- argv++; argc--;
- if (**pp == 0)
- {
- unless (argc > 1) exc_raise(BAD_ARGS);
- *pp = &argv[1][0];
- argv++; argc--;
- }
- }
-
- if (TimerPeriodString != NULL)
- TimerPeriod = atoi(TimerPeriodString);
- if (TimerPeriod == 0) exc_raise(BAD_ARGS);
-
- while (argc > 1 && IsSymFileName (argv[1]))
- {
- char *fname = findfile(path, argv[1], ".sym", FALSE, FALSE);
- LoadSymFile(fname);
- argv++; argc--;
- }
- DefineSegment ((LPSTR) "*BIOS*", 0, 0xf000, 0xffff, 0xf000);
- DefineSegment ((LPSTR) "*DOS*", 0, 0x70, 0xffff, 0x70);
- DefineSegment ((LPSTR) "*EGA*", 0, 0xc000, 0xffff, 0xc000);
-
- strcpy (cmdline, RunFile);
- while (argc > 1)
- {
- strcat(cmdline, " ");
- strcat(cmdline, argv[1]);
- argv++; argc--;
- }
-
- LockTimer(TRUE);
- InitDebugSeg (TimerPeriod);
- fprintf(stderr, "Running: %s\n", cmdline);
- system(cmdline);
- LockTimer(TRUE);
-
- ClearDebugSeg();
- fprintf(stderr, "Statistics...\n\n");
- PrintSegments();
- fprintf(stderr, "Finished\n");
- EXCEPTION
- WHEN(OUT_OF_MEMORY OR_WHEN CONTROL_C OR_WHEN BAD_SYMBOL)
- fprintf(stderr, "%s\n", (char *) exc_code);
- exit(1);
- WHEN(BAD_ARGS)
- fprintf(stderr, "%s\n", (char *) exc_code);
- fprintf(stderr, Usage, progname);
- fprintf(stderr, Copyright);
- exit(1);
- WHEN(FINDFILE_FAILED)
- fprintf(stderr, "%s: %s\n", (char *) exc_code, BadFile);
- exit(1);
- WHEN(OP_FAILED)
- fprintf(stderr, "%s ", progname);
- if (BadFile == NULL) BadFile = "";
- perror(BadFile);
- exit(1);
- WHEN (OTHERS)
- fprintf(stderr, "Unexpected exception: %s\n", (char *) exc_code);
- exit(1);
- END
- exit(0);
- }
-